define(function () {
    return function (pane) {
        var loadData = function () {
            //加载模板
            pane.html(utils.getHtml('admin_analyses'));

            adminApi('analyses_dashboard').then(function (data) {
                var orders = data.orders;
                var users = data.users;
                var agencys = data.agencys;
                var withdraws = data.withdraws;

                for (var i in orders) {
                    pane.find('.orders_' + i + '_count').text(orders[i].count);
                    pane.find('.orders_' + i + '_feeString').text(orders[i].feeString);
                    pane.find('.orders_' + i + '_totalAlipayFeeString').text(orders[i].totalAlipayFeeString);
                }
                for (var i in users) {
                    pane.find('.users_' + i + '_count').text(users[i].count);
                }
                for (var i in agencys) {
                    pane.find('.agency_' + i + '_count').text(agencys[i].count);
                }
                for (var i in withdraws) {
                    pane.find('.withdraws_' + i + '_count').text(withdraws[i].count);
                    pane.find('.withdraws_' + i + '_total_amount').text(withdraws[i].total_amount);
                }
            });

            adminApi('analyses_orders_data').then(function (data) {
                var countData = data.countData;
                var feeStringData = data.feeStringData;
                var totalAlipayFeeStringData = data.totalAlipayFeeStringData;

                var rgbs = ['255, 99, 132', '255, 159, 64', '255, 205, 86', '75, 192, 192', '54, 162, 235', '153, 102, 255', '201, 203, 207'];
                var bds = ['255, 99, 132', '255, 159, 64', '255, 205, 86', '75, 192, 192', '54, 162, 235', '153, 102, 255', '201, 203, 207'];

                var getDatasets = function (map) {
                    var datasets = [], idx = 0, allRowData = [], rgb, bd;
                    for (var i in map) {
                        rgb = rgbs[idx];
                        bd = bds[idx];

                        var rowData = map[i];
                        for (var k in rowData) {
                            if (allRowData[k]) {
                                allRowData[k] += rowData[k];
                            } else {
                                allRowData[k] = rowData[k];
                            }
                        }

                        datasets.push({
                            label: i,
                            fill: false,
                            backgroundColor: "rgba(" + rgb + ",0.4)",
                            borderColor: "rgba(" + rgb + ",1)",
                            pointBorderColor: "rgba(" + rgb + ",1)",
                            pointHoverBackgroundColor: "rgba(" + rgb + ",1)",
                            pointHoverBorderColor: "rgba(" + bd + ",1)",
                            pointBackgroundColor: "#fff",
                            data: rowData
                        });
                        idx++;
                    }

                    for (var i in allRowData) {
                        allRowData[i] = allRowData[i].toFixed(2);
                    }

                    rgb = rgbs[idx];
                    bd = bds[idx];
                    datasets.push({
                        label: '全部',
                        fill: false,
                        backgroundColor: "rgba(" + rgb + ",0.4)",
                        borderColor: "rgba(" + rgb + ",1)",
                        pointBorderColor: "rgba(" + rgb + ",1)",
                        pointHoverBackgroundColor: "rgba(" + rgb + ",1)",
                        pointHoverBorderColor: "rgba(" + bd + ",1)",
                        pointBackgroundColor: "#fff",
                        data: allRowData
                    });

                    return datasets;
                }

                new Chart('ordersCountLineChart', {
                    type: 'line',
                    data: {
                        labels: countData.labels,
                        datasets: getDatasets(countData.datasets)
                    },
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                        }
                    }
                });

                new Chart('ordersFeeStringLineChart', {
                    type: 'line',
                    data: {
                        labels: countData.labels,
                        datasets: getDatasets(feeStringData.datasets)
                    },
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                        }
                    }
                });

                new Chart('ordersTotalAlipayFeeStringLineChart', {
                    type: 'line',
                    data: {
                        labels: countData.labels,
                        datasets: getDatasets(totalAlipayFeeStringData.datasets)
                    },
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                        }
                    }
                });
            });


            adminApi('analyses_users_data').then(function (data) {
                var countData = data.countData;

                var rgbs = ['255, 99, 132', '255, 159, 64', '255, 205, 86', '75, 192, 192', '54, 162, 235', '153, 102, 255', '201, 203, 207'];
                var bds = ['255, 99, 132', '255, 159, 64', '255, 205, 86', '75, 192, 192', '54, 162, 235', '153, 102, 255', '201, 203, 207'];

                var getDatasets = function (map) {
                    var datasets = [], idx = 0, allRowData = [], rgb, bd;
                    for (var i in map) {
                        rgb = rgbs[idx];
                        bd = bds[idx];

                        var rowData = map[i];
                        for (var k in rowData) {
                            if (allRowData[k]) {
                                allRowData[k] += rowData[k];
                            } else {
                                allRowData[k] = rowData[k];
                            }
                        }

                        datasets.push({
                            label: i,
                            fill: false,
                            backgroundColor: "rgba(" + rgb + ",0.4)",
                            borderColor: "rgba(" + rgb + ",1)",
                            pointBorderColor: "rgba(" + rgb + ",1)",
                            pointHoverBackgroundColor: "rgba(" + rgb + ",1)",
                            pointHoverBorderColor: "rgba(" + bd + ",1)",
                            pointBackgroundColor: "#fff",
                            data: rowData
                        });
                        idx++;
                    }

                    for (var i in allRowData) {
                        allRowData[i] = allRowData[i].toFixed(2);
                    }

                    rgb = rgbs[idx];
                    bd = bds[idx];
                    datasets.push({
                        label: '全部',
                        fill: false,
                        backgroundColor: "rgba(" + rgb + ",0.4)",
                        borderColor: "rgba(" + rgb + ",1)",
                        pointBorderColor: "rgba(" + rgb + ",1)",
                        pointHoverBackgroundColor: "rgba(" + rgb + ",1)",
                        pointHoverBorderColor: "rgba(" + bd + ",1)",
                        pointBackgroundColor: "#fff",
                        data: allRowData
                    });

                    return datasets;
                }

                new Chart('usersCountLineChart', {
                    type: 'line',
                    data: {
                        labels: countData.labels,
                        datasets: getDatasets(countData.datasets)
                    },
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                        }
                    }
                });
            });

            adminApi('analyses_withdraws_data').then(function (data) {
                var countData = data.countData;
                var totalAmountData = data.totalAmountData;

                var rgbs = ['255, 99, 132', '255, 159, 64', '255, 205, 86', '75, 192, 192', '54, 162, 235', '153, 102, 255', '201, 203, 207'];
                var bds = ['255, 99, 132', '255, 159, 64', '255, 205, 86', '75, 192, 192', '54, 162, 235', '153, 102, 255', '201, 203, 207'];

                var getDatasets = function (map) {
                    var datasets = [], idx = 0, allRowData = [], rgb, bd;
                    for (var i in map) {
                        rgb = rgbs[idx];
                        bd = bds[idx];

                        var rowData = map[i];
                        for (var k in rowData) {
                            if (allRowData[k]) {
                                allRowData[k] += rowData[k];
                            } else {
                                allRowData[k] = rowData[k];
                            }
                        }

                        datasets.push({
                            label: i,
                            fill: false,
                            backgroundColor: "rgba(" + rgb + ",0.4)",
                            borderColor: "rgba(" + rgb + ",1)",
                            pointBorderColor: "rgba(" + rgb + ",1)",
                            pointHoverBackgroundColor: "rgba(" + rgb + ",1)",
                            pointHoverBorderColor: "rgba(" + bd + ",1)",
                            pointBackgroundColor: "#fff",
                            data: rowData
                        });
                        idx++;
                    }

                    for (var i in allRowData) {
                        allRowData[i] = allRowData[i].toFixed(2);
                    }

                    rgb = rgbs[idx];
                    bd = bds[idx];
                    datasets.push({
                        label: '全部',
                        fill: false,
                        backgroundColor: "rgba(" + rgb + ",0.4)",
                        borderColor: "rgba(" + rgb + ",1)",
                        pointBorderColor: "rgba(" + rgb + ",1)",
                        pointHoverBackgroundColor: "rgba(" + rgb + ",1)",
                        pointHoverBorderColor: "rgba(" + bd + ",1)",
                        pointBackgroundColor: "#fff",
                        data: allRowData
                    });

                    return datasets;
                }

                new Chart('withdrawsCountLineChart', {
                    type: 'line',
                    data: {
                        labels: countData.labels,
                        datasets: getDatasets(countData.datasets)
                    },
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                        }
                    }
                });

                new Chart('withdrawsTotalAmountLineChart', {
                    type: 'line',
                    data: {
                        labels: totalAmountData.labels,
                        datasets: getDatasets(totalAmountData.datasets)
                    },
                    options: {
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true
                                }
                            }]
                        }
                    }
                });
            });
        }

        $(pane).on('click', '.refresh-btn', loadData);
        loadData();
    }
});